package org.lionel.thread.observer;

public abstract class ObserverableRunnable implements Runnable {

    /**
     * 监听这
     */
    final protected LifeCycleListener listener;

    public ObserverableRunnable(LifeCycleListener listener) {
        this.listener = listener;
    }

    public void notifyChange(final RunnableEvent event) {
        listener.onEvent(event);
    }

    public enum RunnableState {
        RUNNING, ERROR, DONE;
    }

    /**
     * 内部类尽量都是静态的，不然作为成员对象每次创建对象都会复制
     */
    public static class RunnableEvent {

        private final RunnableState state;
        private final Thread thread;
        private final Throwable cause;

        public RunnableEvent(RunnableState state, Thread thread, Throwable cause) {
            this.state = state;
            this.thread = thread;
            this.cause = cause;
        }

        public RunnableState getState() {
            return state;
        }

        public Thread getThread() {
            return thread;
        }

        public Throwable getCause() {
            return cause;
        }
    }
}
